/**
自开发 -> json to form js工具
生成form最终表单
name:表单name
action:表单提交地址
method:提交方式 get / post
json:需要生成的表单dom集合
*/

/**
 * 可拓展json结构
 */
var dic_list = '';
var pro_list = '';
function formStart_sub(data){
    dic_list = data.dic;
    pro_list = data.pro_dic;
    var form = createForm({action:data.action,name:data.name,method:data.method});
    //追加表格数据
    form.append($('<div class="weui-cells weui-cells_form"></div>'));
    //检测该子集是否有值
    var default_value = data.value;
    //获取子集结构
    var json = data.json;
    if(default_value==undefined || default_value.length==0){
        //无值状态 加载默认数据
        var length = json.length;
        if(length==0){
            //这里使用weui样式
            alert('没有该子集显示项!');
            history.back(-1);
            return;
        }
        for(var i in json){
             //检测是否是为时间标签
             if(json[i].type=='DATE'){
                //生成时间节点
                var date_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label for="" class="weui-label"></label></div><div class="weui-cell__bd" style="height:44px;line-height:44px;"></div></div>';
                var div_obj = $(date_html);
                //生成左侧标题
                div_obj.find('div:eq(0)').find('label').html(json[i].caption);
                //生成右侧时间input表单
                var createTimeValue = {
                    name:json[i].name,
                    value:''
                };
                var time_text = createTime(createTimeValue);
                div_obj.find('div:eq(1)').append(time_text);
                form.find('div:eq(0)').append(div_obj);
            }else if(json[i].dictionaries==''){
                var text_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell"><div class="weui-cell__bd"></div></div></div>';
                //生成text节点
                var text_obj = $(text_html);
                //生成左侧标题
                text_obj.find('div:eq(0)').find('label').html(json[i].caption);
                //生成右侧文本input表单
                var createTextValue = {
                    name:json[i].name,
                    value:json[i].value,
                    caption:json[i].caption
                };
                var temp_text = createText(createTextValue);
                text_obj.find('div:eq(1)').append(temp_text);
                form.find('div:eq(0)').append(text_obj);
            }else{
                //生成select节点
                var select_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell__hd"></div>';
                var select_obj = $(select_html);
                //生成左侧标题
                select_obj.find('div:eq(0)').find('label').html(json[i].caption);
                //分析字典 默认值
                var dictionaries = json[i].dictionaries;
                var select_data = {
                    name:json[i].name,
                    dictionaries:dictionaries,
                };
                var select = createSelect(select_data);
                select_obj.find('div:eq(1)').append(select);
                form.find('div:eq(0)').append(select_obj);
            }
        }

        //生成提交按钮 和 返回按钮
        var operation_div = $('<div class="operation_div"></div>');
        if(is_dealt==1){
			var submit = $('<input type="button" class="weui-btn weui-btn_primary" value="已在审批中">');        	
        }else{
        	var submit = $('<input type="submit" class="weui-btn weui-btn_primary" value="提交保存">');
        }
        var callback = $('<input type="button" class="weui-btn weui-btn_disabled weui-btn_default callback_button" value="取消操作">');
        operation_div.append(submit);
        operation_div.append(callback);
        //添加表单隐藏域信息
        form.append($('<input type="hidden" name="table_name" value="'+data.name+'"/>'));
        //标记该子集是否有值 1 有 0 没有
        form.append($('<input type="hidden" name="has_value" value="0"/>'));
        form.find('div:eq(0)').append(operation_div);
        //最终生成dom节点数据
        $('body').append(form);
    }else{
        //存在数据情况处理
        //检测是否有数据
        if(default_value.length==0){
            location.reload();
            return;
        }
        
        //统计总数
        var total = 0;
        var div1 = $('<div class="swiper-container"></div>');
        var div2 = $('<div class="swiper-wrapper"></div>');
        
        //有值状态
        for(var j=0 in default_value){
            total++;
            var temp = $('<div class="swiper-slide"></div>');
            var form = createForm({action:data.action,name:data.name,method:data.method});
            //追加表格数据
            form.append($('<div class="weui-cells weui-cells_form"></div>'));
            var json = default_value[j].info;
            for(var i in json){
                //判断类型
                var json_item = data.json;
                var date_type = json_item[i].type;
                var item_caption = json_item[i].caption;

                //检测是否是为时间标签
                if(date_type=='DATE'){
                    //生成时间节点
                    var date_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label for="" class="weui-label"></label></div><div class="weui-cell__bd" style="height:44px;line-height:44px;"></div></div>';
                    var div_obj = $(date_html);
                    //生成左侧标题
                    div_obj.find('div:eq(0)').find('label').html(item_caption);
                    //生成右侧时间input表单
                    var createTimeValue = {
                        name:json[i].name,
                        value:json[i].value
                    };
                    var time_text = createTime(createTimeValue);
                    div_obj.find('div:eq(1)').append(time_text);
                    form.find('div:eq(0)').append(div_obj);
                }else if(date_type=='VARCHAR'){
                    var text_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell"><div class="weui-cell__bd"></div></div></div>';
                    //生成text节点
                    var text_obj = $(text_html);
                    //生成左侧标题
                    text_obj.find('div:eq(0)').find('label').html(item_caption);
                    //生成右侧文本input表单
                    var createTextValue = {
                        name:json[i].name,
                        value:json[i].value,
                        caption:item_caption
                    };
                    var temp_text = createText(createTextValue);
                    text_obj.find('div:eq(1)').append(temp_text);
                    form.find('div:eq(0)').append(text_obj);
                }else{
                    //生成select节点
                    var select_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell__hd"></div>';
                    var select_obj = $(select_html);
                    //生成左侧标题
                    select_obj.find('div:eq(0)').find('label').html(item_caption);
                    //分析字典 默认值
                    var dictionaries = json_item[i].dictionaries;
                    var select_name = json[i].name;
                    select_value = json[i].value;
                    var select_data = {
                        name:select_name,
                        dictionaries:dictionaries,
                        default_value:select_value
                    };

                    var select = createSelect(select_data);
                    select_obj.find('div:eq(1)').append(select);
                    form.find('div:eq(0)').append(select_obj);
                }
            }
            
            //生成提交按钮 和 返回按钮
            var operation_div = $('<div class="operation_div_2"></div>');
            if(is_dealt!=1){
            	var submit = $('<input type="submit" class="weui-btn weui-btn_primary" value="保存">');
            }
            if(is_dealt==1){
            	var callback = $('<input type="button" class="weui-btn weui-btn_primary callback_button" value="返回">');
            }else{
				var callback = $('<input type="button" class="weui-btn weui-btn_primary callback_button" value="返回">');            	
            }
            if(is_dealt!=1){
            	var add = $('<input type="button" class="weui-btn weui-btn_primary add_info" value="新增">');
                var del = $('<input _offset_id='+j+' type="button" class="weui-btn weui-btn weui-btn_warn sub_del" value="删除">');
            }
            operation_div.append(submit);
            operation_div.append(callback);
            operation_div.append(add);
            operation_div.append(del);

            //添加表单隐藏域信息
            form.append($('<input type="hidden" name="table_name" value="'+data.name+'"/>'));
            form.append($('<input type="hidden" name="has_value" value="1"/>'));
            //追加当前记录位置id
            form.append($('<input type="hidden" name="_offset_id" value="'+j+'"/>'));
            form.find('div:eq(0)').append(operation_div);
            //最终生成dom节点数据
            temp.append(form);
            div2.append(temp);
        }
        
        var page = $('<div style="position:fixed;top:-3px;z-index:0;" class="swiper-pagination"></div>');
        //判断单条数据情况 如果单条则隐藏蓝点
        if(total!=1){
            div1.append(page);
        }
        div1.append(div2);
        $('body').append(div1);
    }
}

/**
 * 非可拓展json结构 员工基本信息
 */
function formStart(data){
	dic_list = data.dic;
    pro_list = data.pro_dic;
    var form = createForm({action:data.action,name:data.name,method:data.method});
    //追加表格数据
    form.append($('<div class="weui-cells weui-cells_form"></div>'));
    //获取子集数据
    var json = data.json;
    for(i in json){
        //检测是否是为时间标签
        
        if(json[i].type=='DATE'){
            //生成时间节点
            var date_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label for="" class="weui-label"></label></div><div class="weui-cell__bd" style="height:44px;line-height:44px;"></div></div>';
            var div_obj = $(date_html);
            //生成左侧标题
            div_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //生成右侧时间input表单
            var createTimeValue = {
                name:json[i].name,
                value:json[i].value
            };
            var time_text = createTime(createTimeValue);
            div_obj.find('div:eq(1)').append(time_text);
            form.find('div:eq(0)').append(div_obj);
        }else if(json[i].dictionaries==''){
            var text_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell"><div class="weui-cell__bd"></div></div></div>';
            //生成text节点
            var text_obj = $(text_html);
            //生成左侧标题
            text_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //生成右侧文本input表单
            var createTextValue = {
                name:json[i].name,
                value:json[i].value,
                caption:json[i].caption
            };
            var temp_text = createText(createTextValue);
            text_obj.find('div:eq(1)').append(temp_text);
            form.find('div:eq(0)').append(text_obj);
        }else{
            //生成select节点
            var select_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell__hd"></div>';
            var select_obj = $(select_html);
            //生成左侧标题
            select_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //分析字典 默认值
            var dictionaries = json[i].dictionaries;
            var select_name = json[i].name;
            var select_value = json[i].value;
            var select_data = {
                name:select_name,
                dictionaries:dictionaries,
                default_value:select_value,
            };
            var select = createSelect(select_data);
            select_obj.find('div:eq(1)').append(select);
            form.find('div:eq(0)').append(select_obj);
        }
    };
    //生成提交按钮 和 返回按钮
    var operation_div = $('<div class="operation_div"></div>');
    if(is_dealt==1){
        var submit = $('<input type="submit" class="weui-btn weui-btn_primary" value="已在审批中">');        
    }else{
        var submit = $('<input type="submit" class="weui-btn weui-btn_primary" value="提交保存">');
    }
    var callback = $('<input type="button" class="weui-btn weui-btn_disabled weui-btn_default callback_button" value="取消操作">');
    operation_div.append(submit);
    operation_div.append(callback);
    //添加表单隐藏域信息
    form.append($('<input type="hidden" name="table_name" value="'+data.name+'"/>'));
    form.find('div:eq(0)').append(operation_div);
    //最终生成dom节点数据
    $('body').append(form);
}

/**
 * 添加新记录数据 
 */
function addNew(data){
	dic_list = data.dic;
    pro_list = data.pro_dic;
    var form = createForm({action:data.action,name:data.name,method:data.method});
    //追加表格数据
    form.append($('<div class="weui-cells weui-cells_form"></div>'));
    var json = data.json;
    var length = json.length;
    //检测是否有数据
    if(length==0){
        alert('没有该子集显示项!');
        history.back(-1);
        return;
    }
    for(var i=0;i<length;i++){
        //检测是否是为时间标签
        if(json[i].type=='DATE'){
            //生成时间节点
            var date_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label for="" class="weui-label"></label></div><div class="weui-cell__bd" style="height:44px;line-height:44px;"></div></div>';
            var div_obj = $(date_html);
            //生成左侧标题
            div_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //生成右侧时间input表单
            var createTimeValue = {
                name:json[i].name,
                value:''
            };
            var time_text = createTime(createTimeValue);
            div_obj.find('div:eq(1)').append(time_text);
            form.find('div:eq(0)').append(div_obj);
        }else if(json[i].dictionaries==''){
            var text_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell"><div class="weui-cell__bd"></div></div></div>';
            //生成text节点
            var text_obj = $(text_html);
            //生成左侧标题
            text_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //生成右侧文本input表单
            var createTextValue = {
                name:json[i].name,
                value:'',
                caption:json[i].caption
            };
            var temp_text = createText(createTextValue);
            text_obj.find('div:eq(1)').append(temp_text);
            form.find('div:eq(0)').append(text_obj);
        }else{
            //生成select节点
            var select_html = '<div class="weui-cell weui-cell_vcode"><div class="weui-cell__hd"><label class="weui-label"></label></div><div class="weui-cell__hd"></div>';
            var select_obj = $(select_html);
            //生成左侧标题
            select_obj.find('div:eq(0)').find('label').html(json[i].caption);
            //分析字典 默认值
            var dictionaries = json[i].dictionaries;
            var select_data = {
                name:json[i].name,
                dictionaries:dictionaries,
            };
            var select = createSelect(select_data);
            select_obj.find('div:eq(1)').append(select);
            form.find('div:eq(0)').append(select_obj);
        }
    }
    var operation_div = $('<div class="operation_div"></div>');
    var submit = $('<input type="submit" class="weui-btn weui-btn_primary" value="提交保存">');
    var callback = $('<input type="button" class="weui-btn weui-btn_disabled weui-btn_default callback_button" value="取消操作">');
    operation_div.append(submit);
    operation_div.append(callback);

    //添加表单隐藏域信息
    form.append($('<input type="hidden" name="table_name" value="'+data.name+'"/>'));
    form.find('div:eq(0)').append(operation_div);
    //最终生成dom节点数据
    $('body').append(form);
}

/**
 * 生成form表单对象
 */
function createForm(args){
    var dom = $("<form name='"+args.name+"' action='"+args.action+"' method='"+args.method+"'></form>");
    return dom;
}

/**
 * 生成text文本表单对象
 */
function createText(data){
    var text_input = $('<input class="weui-input" type="text" placeholder="请输入'+data.caption+'"/>');
    text_input.attr({
        name:data.name,
        value:data.value,
    });
    return text_input;
}

/*
 * 生成时间对象
 */
function createTime(data){
    time = data.value;
    var time_input = $('<input style="padding-left:15px;" class="weui-input" type="date" />');
    time_input.attr({
        name:data.name,
        value:time,
    });
    return time_input;
}

/**
 * 生成下拉select对象
 */
function createSelect(data){
    var select_input = $('<select class="weui-select" name="select1"></select>');
    select_input.attr({
        name:data.name,
        dictionaries:data.dictionaries,
    });
    
	//省份情况
	if(data.name=='JG' || data.name=='SF'){
	    var select_input = getProvince(pro_list,data.default_value,select_input);
	    return select_input;
	}else if(data.name=='CS' || data.name=='CSD'){
//      if(data.name=='CS'){
//          CS = data.default_value;
//      }else{
//          CSD = data.default_value;
//      }
//      var select_input = getCity(city_list,data.default_value,select_input);
        return select_input;
	}else{
    //正常情况
        var dictionaries = data.dictionaries;
        var select_input = getDictionaries(dictionaries,data.default_value,select_input);
        return select_input;
    }
}

/*
 * 获取省份下拉
 */
function getProvince(pro_list,default_value,select_obj){
    //添加请选择
    //判断默认值否是为空
    var fir_option = $('<option>请选择</option>');
    //排除是否选项
    if(default_value==''){
        var empty_option = $('<option value="" selected>无</option>');   
    }else{
        var empty_option = $('<option value="">无</option>');
    }       
    select_obj.append(fir_option);
    select_obj.append(empty_option);
    for(var i=0;i<pro_list.length;i++){
        var option = $('<option></option>');
        //判断是否显示
        if(pro_list[i].display==true){
            option.attr({
                value:pro_list[i].value,
            });
            //默认值选中
            if(pro_list[i].value==default_value){
                option.attr({
                    selected:true,
                });
            }
            option.html(pro_list[i].caption);
            select_obj.append(option);
        }
    }
    return select_obj;
}

/**
 * 获取市区下拉
 */
function getCity(city_list,default_value,select_obj){
    //添加请选择
    //判断默认值否是为空
    var fir_option = $('<option>请选择</option>');
    //排除是否选项
    if(default_value==''){
        var empty_option = $('<option value="" selected>无</option>');   
    }else{
        //有值
        var empty_option = $('<option value="">无</option>');
    }       
    select_obj.append(fir_option);
    select_obj.append(empty_option);
    return select_obj;
}

/**
 * 获取字典下拉
 */
function getDictionaries(id,default_value,select_obj){
	//id 转意操作 . -> _
	id = id.replace(/\./g,'_');
	
    //判断默认值否是为空
    var fir_option = $('<option>请选择</option>');
    //添加无
    //检测默认值是否为空''
    //排除是否选项
    if(id!='50000001' && id!='50000002' && id!='81794617'){
		if(default_value=='' || default_value=='无'){
    		var empty_option = $('<option value="无" selected>无</option>');	
	    }else{
	    	var empty_option = $('<option value="无">无</option>');
	    }    	
    }
    //添加默认项节点
    select_obj.append(fir_option);
    select_obj.append(empty_option);
    
    for(var i=0;i<dic_list.length;i++){
    	var i_name = dic_list[i]['name'];
    	if(id=='XB'){
    		id = 'GB_SEX';
    	}
        if(i_name == id){
           var obj = dic_list[i];
           break;
        }
    }
   
    var dic_subitem = obj.subitem;
    
    for(var j=0;j<dic_subitem.length;j++){
        var option = $('<option></option>');
        //判断是否显示
        if(dic_subitem[j].display==true){
            option.attr({
                value:dic_subitem[j].value,
            });
            //默认值选中
            if(dic_subitem[j].value==default_value){
                option.attr({
                    selected:true,
                });
            }
            option.html(dic_subitem[j].caption);
            select_obj.append(option);
        }
    }
    return select_obj;
}